package example;

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Vector;

import jvn.client.annotation.ReadAndWrite;
import jvn.client.annotation.ReadOnly;

public class SharedObject extends Vector<Integer> implements Comparable<Integer> {

	public final static String CLASSNAME = "SharedObject";
	private String name;
	protected int results;
	private File file;
	private static int nbInstanciation = 0;

	public SharedObject() {
		name = "Default";
		results = 0;
		this.file = null;
		nbInstanciation += 1;

	}

	public SharedObject(String name, int results) {
		super();
		this.name = name;
		this.results = results;
		this.file = null;

		nbInstanciation += 1;

	}

	public void division(int nb1, int nb2) throws ArithmeticException {
		if (nb2 == 0)
			throw new ArithmeticException("Divised by 0");
		negative(nb1);
		results = nb1 / nb2;
		Double res = Math.sqrt(results);
		results = res.intValue();
	}

	private int negative(int nb) {
		return -nb;

	}

	@ReadAndWrite
	public void loadFile(String path) throws NullPointerException {
		if (path == null)
			throw new NullPointerException();
		file = new File(path);
	}

	@ReadAndWrite
	public void empile() {
		add(results);
	}

	@ReadAndWrite
	public int depile() throws NoSuchElementException {
		if (this.size() == 0)
			throw new NoSuchElementException();
		return this.get(0);
	}

	@ReadAndWrite
	public int getElement(int index) throws NoSuchElementException, IndexOutOfBoundsException {
		if (this.size() == 0)
			throw new NoSuchElementException();
		if (this.size() < index || index < 0)
			throw new IndexOutOfBoundsException();
		return this.get(index);

	}

	@ReadOnly
	public String getName() {
		return name;
	}

	@ReadAndWrite
	public void setName(String name) {
		this.name = name;
	}

	@ReadOnly
	public int getResults() {
		return results;
	}

	@ReadAndWrite
	public void setResults(int results) {
		this.results = results;
	}

	@ReadOnly
	public File getFile() {
		return file;
	}

	@ReadAndWrite
	public void setFile(File file) {
		this.file = file;
	}

	@ReadOnly
	public synchronized int compareTo(Integer val) {
		if (val == results)
			return 0;
		else if (val < results)
			return -1;
		else
			return 1;
	}

	@ReadOnly
	public static int getNbInstanciation() {
		return nbInstanciation;
	}

	@ReadAndWrite
	public static void setNbInstanciation(int nbInstanciation) {
		SharedObject.nbInstanciation = nbInstanciation;
	}

	@ReadOnly
	public static String getClassname() {
		return CLASSNAME;
	}

}
